package com.lucky.util;

import com.lucky.pojo.vo.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicLong;

/**
 * @Description 模拟数据库存储数据
 * @Author Jalyson
 * @Version V1.0.0
 * @Since 1.0
 * @Date 2019/6/26
 */
public class UserDBUtil {
    /** logger */
    private static final Logger LOGGER = LoggerFactory.getLogger(UserDBUtil.class);
    /**
     * 计数器
     */
    private final static AtomicLong counter = new AtomicLong(0);
    private static Map<Long, User> dataMap;

    static {
        if (dataMap == null) {
            dataMap = new HashMap<>();
        }
        User user = new User();
        user.setId(counter.getAndIncrement());
        user.setAge(29);
        user.setUsername("Jalyson");
        user.setPassword("Jalyson123");
        user.setBrief("haha.");
        user.setRole("admin,manager");
        dataMap.put(user.getId(), user);
        user = new User();
        user.setId(counter.getAndIncrement());
        user.setAge(29);
        user.setUsername("kaka");
        user.setPassword("kaka123");
        user.setBrief("haha.");
        user.setRole("admin,manager");
        dataMap.put(user.getId(), user);

    }

    /**
     * findAll
     */
    public static Collection<User> findAll() {
        LOGGER.info("UserDBUtil.findAll()");
        return dataMap.values();
    }

    /**
     * findAll
     */
    public static User findByUsernameAndPassword(String username, String password) {
        LOGGER.info("UserDBUtil.findByUsernameAndPassword()");
        Collection<User> values = dataMap.values();
        for (User user : values) {
            if (user.getUsername().equals(username) && user.getPassword().equals(password)) {
                return user;
            }
        }
        return null;
    }

    /**
     * 保存
     * @param obj 保存数据
     */
    public static User save(User obj) {
        LOGGER.info("UserDBUtil.save()");
        if (dataMap == null) {
            dataMap = new HashMap<>();
        }
        if (obj.getPassword() == null || obj.getUsername() == null) {
            return null;
        }
        Collection<User> values = dataMap.values();
        for (User user : values) {
            if (user.getUsername().equals(obj.getUsername()) && user.getPassword().equals(obj.getPassword())) {
                return null;
            }
        }

        if (obj.getId() == null) {
            obj.setId(counter.getAndIncrement());
        }
        dataMap.put(obj.getId(), obj);
        return obj;
    }

    /**
     * 获取缓存数据
     * @return
     */
    public static User get(Long id) {
        LOGGER.info("UserDBUtil.get()");
        if (dataMap == null || id == null) {
            return null;
        }
        return dataMap.get(id);
    }

    /**
     * 删除数据
     * @param id id
     * @return
     */
    public static boolean remove(Long id) {
        LOGGER.info("UserDBUtil.remove()");
        if (dataMap == null || id == null) {
            return false;
        }
        return dataMap.remove(id) != null;
    }

}
